package com.ruoyi.system.service.impl;

import java.util.*;

import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysAreaMapper;
import com.ruoyi.system.domain.SysArea;
import com.ruoyi.system.service.ISysAreaService;

/**
 * 地区Service业务层处理
 *
 * @author ruoyi
 * @date 2023-02-07
 */
@Service
public class SysAreaServiceImpl implements ISysAreaService {
    @Autowired
    private SysAreaMapper sysAreaMapper;

    /**
     * 查询地区
     *
     * @param id 地区主键
     * @return 地区
     */
    @Override
    public SysArea selectSysAreaById(Long id) {
        return sysAreaMapper.selectSysAreaById(id);
    }

    /**
     * 查询地区列表
     *
     * @param sysArea 地区
     * @return 地区
     */
    @Override
    public List<SysArea> selectSysAreaList(SysArea sysArea) {
        return sysAreaMapper.selectSysAreaList(sysArea);
    }

    /**
     * 新增地区
     *
     * @param sysArea 地区
     * @return 结果
     */
    @Override
    public int insertSysArea(SysArea sysArea) {
        return sysAreaMapper.insertSysArea(sysArea);
    }

    /**
     * 修改地区
     *
     * @param sysArea 地区
     * @return 结果
     */
    @Override
    public int updateSysArea(SysArea sysArea) {
        sysArea.setUpdateTime(DateUtils.getNowDate());
        return sysAreaMapper.updateSysArea(sysArea);
    }

    /**
     * 批量删除地区
     *
     * @param ids 需要删除的地区主键
     * @return 结果
     */
    @Override
    public int deleteSysAreaByIds(Long[] ids) {
        return sysAreaMapper.deleteSysAreaByIds(ids);
    }

    /**
     * 删除地区信息
     *
     * @param id 地区主键
     * @return 结果
     */
    @Override
    public int deleteSysAreaById(Long id) {
        return sysAreaMapper.deleteSysAreaById(id);
    }

    /**
     * * 获取地区列表带索引
     * @param sysArea
     * @return
     */
    @Override
    public List<Map<String, Object>> getAreaList(SysArea sysArea) {
        List<SysArea> sysAreas = sysAreaMapper.selectSysAreaList(sysArea);
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, List<String>> groupedMap = new LinkedHashMap<>();
        for (SysArea sysArea1 : sysAreas) {
            String cityIndex = sysArea1.getCityIndex();
            groupedMap.computeIfAbsent(cityIndex, k -> new ArrayList<>()).add(sysArea1.getName());
        }
        groupedMap.forEach((letter, data) -> {
            Map<String, Object> map = new HashMap<>();
            map.put("letter", letter);
            map.put("data",data);
            list.add(map);
        });
        return list;
    }

    @Override
    public Long getId(SysArea sysArea) {
        return sysAreaMapper.getId(sysArea);
    }
}
